package com.medaxis.hpams.sso.service.impl;


import com.medaxis.hpams.sso.domain.User;
import com.medaxis.hpams.sso.domain.dto.UserDto;
import com.medaxis.hpams.sso.mapper.UserRoleMapper;
import com.medaxis.hpams.sso.service.MenuService;
import com.medaxis.hpams.sso.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * 用户权限处理
 */
@Component
public class PermissionService {
    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserRoleMapper UserRoleMapper;


    /**
     * 获取菜单数据权限
     *
     * @param user 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(UserDto userDto) {
        Set<String> perms = new HashSet<String>();
        // 查询当前用户的角色
        Long i =UserRoleMapper.getRoleIdByUserID(userDto.getUserId());
        // 管理员拥有所有权限
        if (userDto.isAdmin(userDto.getUserId()) || i==1L) {
            perms.add("*:*:*");
        } else {
            perms.addAll(menuService.selectMenuPermsByUserId(userDto.getUserId()));
        }
        return perms;
    }
}
